import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/model/partner_count_data_model.dart';
import 'package:flutter_app_unionad/model/partner_dev_list_model.dart';
import 'package:flutter_app_unionad/model/partner_income_list_model.dart';
import 'package:flutter_app_unionad/model/partner_ship_info_model.dart';
import 'package:flutter_app_unionad/model/partner_ship_pay_model.dart';
import 'package:flutter_app_unionad/request/base_request.dart';

class PartnerShipRequest {
  //获取设备信息
  static Future<PartnerShipInfoModel?> getPartnerShipRequest() async {
    final future = await HttpRequest.getRequest('/api/partner/getPartnerInfo');
    PartnerShipInfoModel? model;
    baseRequestAnalysisData(future, (map) {
      //
      model = PartnerShipInfoModel.fromJson(map);
    }, (info, code) {});
    return model;
  }

  static Future<bool> savePartnerShipRequest({
    String? address,
    String? idNo,
    String? mobile,
    String? name,
  }) async {
    final future = await HttpRequest.postRequest('/api/partner/savePartnerInfo', params: {
      "address": address,
      "idNo": idNo,
      "mobile": mobile,
      "name": name,
    });
    bool model = false;
    baseRequestAnalysisData(future, (map) {
      //
      model = true;
    }, (info, code) {});
    return model;
  }

  //购买设备
  static Future<PartnerShipPayModel?> partnerShipPayRequest({
    String? num,
    String payType = "1", //1微信 2支付宝  0 收益直接购买
  }) async {
    final future = await HttpRequest.postRequest('/api/partner/pay', params: {
      "num": num,
      "payType": payType,
    });
    PartnerShipPayModel? model;
    baseRequestAnalysisData(future, (map) {
      //
      model = PartnerShipPayModel.fromJson(map);
    }, (info, code) {});
    return model;
  }

  //获取合伙人统计数据
  static Future<PartnerCountDataModel?> partnerCountDataRequest() async {
    final future = await HttpRequest.getRequest('/api/partner/getPartnerCountData');
    PartnerCountDataModel? model;
    baseRequestAnalysisData(future, (map) {
      //
      model = PartnerCountDataModel.fromJson(map);
    }, (info, code) {});
    return model;
  }

  ///获取合伙人设备列表
  static Future<List<PartnerDevListModel>?> getPartnerDevList() async {
    final future = await HttpRequest.postRequest(
      '/api/partner/getPartnerDevList',
      params: {"pageNum": 1, "pageSize": '1000'},
    );
    List<PartnerDevListModel> list = [];
    baseRequestAnalysisData(future, (map) {
      for (var m in map['list']) {
        list.add(PartnerDevListModel.fromJson(m));
      }
    }, (info, code) {});
    return list;
  }

  //获取合伙人收益明显
  static Future<List<PartnerIncomeListModel>> getPartnerIncomeList({
    int pageNum = 0,
  }) async {
    DateTime dateTime = DateTime.now()..subtract(Duration(days: 1));

    final future = await HttpRequest.postRequest(
      '/api/partner/getPartnerIncomeList',
      params: {
        "pageNum": pageNum,
        "pageSize": '20',
        // 'endDate': '${dateTime.year}-${dateTime.month}-${dateTime.day}',
        // "startDate": '${dateTime.year}-${dateTime.month}-${dateTime.day}',
      },
    );
    List<PartnerIncomeListModel> list = [];
    baseRequestAnalysisData(future, (map) {
      for (var m in map['list']) {
        list.add(PartnerIncomeListModel.fromJson(m));
      }
    }, (info, code) {});
    return list;
  }

  //获取系统设置
  static Future<String?> getConfigBykey(
    int index, //0 最大购买数量，1 客服电话, 2 设备单价
  ) async {
    if (index > 2) return null;
    List<String> list = [
      'com.niuniu.app_max_buy_device_amount',
      'com.niuniu.app_service_phone',
      'com.niuniu.app_dev_price '
    ];
    final future = await HttpRequest.getRequest('/api/user/getConfigByKey', params: {'key': list[index]});
    String? result;
    baseRequestAnalysisData(future, (data) {
      result = '$data';
    }, (info, code) {
      ShowToastWidget.showToast(msg: '购买失败');
    });
    return result;
  }
}
